﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MapgisEbp.QuickDataBase;
using Logistics.DataManage.Local;
using Logistics.DataManage.DAL;
using System.Data;

namespace Logistics.DataManage
{
    public class RatesDetailDAO
    {
        public static ORatesDetail GetDetailIdByRateIdAndTagVal(String rateId, float TagVal)
        {
            String sql = "select * from RatesDetailInfo rf where rf.RateSid=? and cast(MinLimit as float)<=? and cast(MaxLimit as float)>=? ";
            ParameterizedSQL PSQL = LocalDb.Handler.DbAdapter.AdaptSQLAnonymousParams(sql, rateId, TagVal, TagVal);
            DataSet ds = LocalDb.Handler.ExecuteQuery(PSQL.SQL, PSQL.Parameters);
            IList<ORatesDetail> list = ConvertTo(ds);
            return list.Count > 0 ? list[0] : null;
        }

        private static IList<ORatesDetail> ConvertTo(DataSet ds)
        {
            IList<ORatesDetail> list = new List<ORatesDetail>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    ORatesDetail RateDel = new ORatesDetail();
                    RateDel.RatesDetailsID = Convert.ToString(row["RatesDetailsID"]);
                    if (row["RatesInforMode"]!=DBNull.Value)
                        RateDel.RatesInforMode = Convert.ToInt32(row["RatesInforMode"]);
                    if (row["State"] != DBNull.Value)
                        RateDel.State = Convert.ToInt32(row["State"]);
                    if (row["Rates"] != DBNull.Value)
                        RateDel.Rates = Convert.ToSingle(row["Rates"]);
                    if (row["Amount"] != DBNull.Value)
                        RateDel.Amount = Convert.ToSingle(row["Amount"]);
                    if (row["LowerLimit"] != DBNull.Value)
                        RateDel.LowerLimit = Convert.ToSingle(row["LowerLimit"]);
                    if (row["UpperLimit"] != DBNull.Value)
                        RateDel.UpperLimit = Convert.ToSingle(row["UpperLimit"]);
                    if (row["MinLimit"] != DBNull.Value)
                        RateDel.MinLimit = Convert.ToSingle(row["MinLimit"]);
                    if (row["MaxLimit"] != DBNull.Value)
                        RateDel.MaxLimit = Convert.ToSingle(row["MaxLimit"]);
                    list.Add(RateDel);
                }
            }
            return list;
        }
    }
}
